package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.kernelab.basis.DataReader;
import org.kernelab.basis.DataWriter;
import org.kernelab.basis.Tools;

import com.sinotrust.classify.algorithm.Svm;
import com.sinotrust.classify.mba.Term;

import svm.svm_predict;
import svm.svm_train;

public class Search {
	// private static final Result Result = null;
	// IndexSearcher is = new IndexSearcher
	/**
	 * @param args
	 * @throws IOException
	 * @throws CorruptIndexException
	 * 
	 * 
	 */
	// private static Map<String, com.sinotrust.knn.vsm.Document> docs;
	public static void main(String[] args) throws CorruptIndexException,
			IOException {
//		DataOperation dataOper = new DataOperation();
//		dataOper.negativeIndex="indexnonindexCommercial";
//		dataOper.positiveIndex="indexCommercial";
//		dataOper.positiveData="positive.txt";
//		dataOper.negativeData="negative.txt";
//		dataOper.freStatic();
//		dataOper.jiao();
//		dataOper.resultWriter();
//		IndexReader ir = IndexReader.open("indexnonindexCommercial");
//		dataOper.indexReverse(ir);
		
//		double[] r = dataOper.CoverageResult(ir, "Commerce.txt");
//		Tools.debug(r);
//		demo
		
		
////		
		Svm st = new Svm();
//		st.parametertrain = "-b 1 -c 32 -g 0.0004"; 
//		
//		st.inputdatafile = "dataSvm.txt";
//		st.model = "model_file";		
////		st.train();
//		st.parameterpredict = "-b 1";
//		st.testFile = "dataSvm.txt";
//		st.predict();
		st.analyser("dataSvmTest.txt",0.80);
//		st.errorDataOutput("dataSvm.txt","output_file");
//		st.train("newdataclassify.txt");
		
		// witerInSVM(jiaor, 1);
		// double[] fr1 = featureGet(r1, jiaor);
		//
		// double[] fr2 = featureGet(r2, jiaor);
		// termEnum.next();
		// Tools.debug(termEnum.term());
		// TermDocs termDocs = ir.termDocs(termEnum.term());

		// while (termDocs.next()) {
		// // Tools.debug(termEnum.term().text());
		// Tools.debug(termDocs.freq());
		// }
		// for (int i = 0; i < 20000; i++) {
		// termEnum.next();
		// Tools.debug(termEnum.term().toString());
		// }

		// Set<Entry<String, Double>> set = result.entrySet();
		// for (Map.Entry<String, Double> tm : set){
		// System.out.println(tm.getKey()+"\t"+tm.getValue());
		// }
		// Tools.debug(termEnum.term());
		// TermDocs termDocs = ir.termDocs(termEnum.term());

		// System.out.println("列表");
		// for (int i = 0; i < ir.numDocs(); i++) {
		// com.sinotrust.knn.vsm.Document doc = new
		// com.sinotrust.knn.vsm.Document();
		// doc.setTitle(ir.document(i).getField("title").stringValue());
		// doc.setUrl(ir.document(i).getField("url").stringValue());
		// doc.setcCode("0001");
		// doc.term = new List<Term>();
		// docs.put("0001" + String.valueOf(i), doc);
		// }

		// System.out.println(ir.numDocs());
		// for (int i = 0 ; i<ir.numDocs();i++){
		// System.out.println(ir.document(i));
		// }
		// doc = ir.document(1).getField("title").stringValue();

		// Enumeration fields = ir.document(1).fields();
		// while (fields.hasMoreElements()){
		// Field field = (Field)fields.nextElement();
		// String fieldName = field.name();
		// Tools.debug(fieldName);
		// }

		// IndexSearcher s = new IndexSearcher("西行漫记index");
		// Term t = new Term("content","我");
		// Query q = new TermQuery(t);
		// TermDocs termDocs = s.getIndexReader().termDocs(t);
		// while(termDocs.next()){
		// System.out.println(termDocs.freq());
		// System.out.println(s.getIndexReader().document(termDocs.doc()));
		// }

		// Hits hits = s.search(q);
		// for (int i = 0 ; i<hits.length(); i++){
		// System.out.println(hits.doc(i));
		// }
		// s.close();

		// Set<Entry<String, Double>> set = result.entrySet();
		// for (Map.Entry<String, Double> tm : set){
		// System.out.println(tm.getKey()+"\t"+tm.getValue());
		// }

		// BufferedWriter bw = new BufferedWriter(new File)
		// Set<Result> set = result;
		// for (Result tm :result.descendingSet()){
		// System.out.println(tm.key+"\t"+tm.value);
		// }
		// witerInSVM(result, 1);

	}



	private static void mergeFile(String string, String string2, String string3)
			throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(string));
		DataWriter dw = new DataWriter();
		dw.setDataFile(new File(string3));
		dw.setAutoFlush(false);
		String tmp = br.readLine();
//		Tools.debug(tmp);
		while ((tmp = br.readLine()) != null) {
			// Tools.debug(tmp);
			dw.write(tmp);
		}

		br = new BufferedReader(new FileReader(string2));
		tmp = br.readLine();
		while ((tmp = br.readLine()) != null) {
			dw.write(tmp);
		}

		br.close();
		dw.close();

	}

	public static void featureBring(String string, TreeSet<Term> jiaor,
			int index, String out) throws CorruptIndexException, IOException {
		double value = 0;
		Map<Integer, SvmOutput> setSvmOutput = new HashMap();
		IndexReader ir = IndexReader.open(string);
		 Tools.debug(ir.numDocs());
		//
		for (int i = 0; i < ir.numDocs(); i++) {
			SvmOutput tmp = new SvmOutput();
			for (Term tmp1 : jiaor) {
				 Tools.debug(tmp1.term + "\t" + value);
				// Tools.debug(tmp.relation);
				tmp.relation.put(tmp1.term, value);
			}
			setSvmOutput.put(i, tmp);
		}

		// DataWriter dw = new DataWriter();
		// dw.setDataFile(new File("tmp.txt"));
		// dw.setAutoFlush(false);
		// Set<Entry<Integer, SvmOutput>> set = setSvmOutput.entrySet();
		// for (Map.Entry<Integer, SvmOutput> tmp : set) {
		// Tools.debug(tmp.getKey());
		// dw.print(tmp.getKey() + "\t");
		// for (Result tm : tmp.getValue().relation) {
		// System.out.print(tm.key + "\t");
		// dw.print(tm.key + "\t");
		// }
		// Tools.debug(" ");
		// dw.write(" ");
		// }
		// Add the jiaoset
		Set<String> s = new HashSet<String>();
		for (Term tmp : jiaor) {
			s.add(tmp.term);
		}
		// int n = ir.numDocs();
		// for (int i = 0; i < n; i++) {
		// for (Result t : jiaor){
		// ir.document(i);
		// TermDocs termDocs =Term(t.key);
		// Tools.debug(termDocs.doc());
		// }
		// // Tools.debug(ir.document(i).getField("url")+"\t");
		// }

		// 特征提取 feature brought
		// Tools.debug("============");
		TermEnum termEnum = ir.terms();
		// TreeSet<Result> result = new TreeSet<Result>();
		while (termEnum.next()) {
			TermDocs termDocs = ir.termDocs(termEnum.term());
			// Tools.debug(termDocs);
			while (termDocs.next()) {
				// if termEnum.term().text() in List<term>,termDocs.freq();
				String term = termEnum.term().text();
				if (s.contains(term)) {
					float tf = termDocs.freq();
					for (Entry<Integer, SvmOutput> tmp : setSvmOutput
							.entrySet()) {
						if (tmp.getKey() == termDocs.doc()) {
							for (Entry<String, Double> tmp1 : tmp.getValue().relation
									.entrySet()) {
								if (tmp1.getKey().equals(term)) {
									tmp1.setValue((double) tf);
								}
							}
						}
					}

					// Tools
					// .debug(termDocs.doc() + "\t" + term + "\t" + tf
					// + "\t");
				}
				// boolean keywordFlag = isKeywordFlag(term);
				// float tf = termDocs.freq();
				// Tools.debug(termDocs.doc()+"\t"+term+"\t"+tf+"\t");

			}
		}
		// Tools.debug("============");
		DataWriter dw = new DataWriter();
		dw.setDataFile(new File(out));
		dw.setAutoFlush(false);
		Set<Entry<Integer, SvmOutput>> set = setSvmOutput.entrySet();
		for (Map.Entry<Integer, SvmOutput> tmp : set) {
			int i = 1;
			// System.out.print(index + " ");
			dw.print(index + " ");
			// dw.print(tmp.getKey() + " ");
			for (Entry<String, Double> tm : tmp.getValue().relation.entrySet()) {
				// System.out.print(i + ":" + tm.getValue() + " ");
				dw.print(i + ":" + tm.getValue() + " ");
				// dw.print(i + ":" +tm.getKey()+ tm.getValue() + " ");
				i++;
			}
			// Tools.debug(" ");
			dw.write(" ");
		}
		ir.close();
		dw.close();
		// return result;

	}

	private static double[] featureGet(TreeSet<Term> r1, TreeSet<Term> jiaor) {
		// TODO Auto-generated method stub
		// int n = jiaor.size();
		// for (String tmp : jiaor) {
		// Tools.debug(tmp);
		// }
		// Tools.debug(n);
		double[] r = new double[101];
		int i = 0;
		for (Term tmp : jiaor) {
			System.out.print(tmp + "\t");
			for (Term tr : r1) {
				if (tmp.equals(tr.term)) {
					r[i] = (float) tr.value;
					i = i + 1;
					// Tools.debug(tr.value + "\t" + tr.key);
					// Tools.debug(r);
				}
			}

		}
		// Tools.debug(i);
		return r;
	}

	private static TreeSet<String> bu(TreeSet<Term> r1, TreeSet<Term> jiao) {
		// TODO Auto-generated method stub
		Set<String> s = new HashSet<String>();
		for (Term tmp : jiao) {
			s.add(tmp.term);
		}
		TreeSet<String> r = new TreeSet<String>();
		for (Term tm1 : r1) {
			if (!s.contains(tm1.term)) {
				r.add(tm1.term);
			}
		}

		return r;
	}

	public static void witer(TreeSet<String> jiaor, int i) {
		// TODO Auto-generated method stub
		for (String tm : jiaor.descendingSet()) {
			// br.write(flag + ": " + tm.value);
			System.out.println(tm);
		}

	}

	private static TreeSet<Term> jiao(TreeSet<Term> r1, TreeSet<Term> r2) {
		// TODO Auto-generated method stub
		TreeSet<Term> r = new TreeSet<Term>();
		TreeSet<String> s = new TreeSet<String>();
		// int i = 0;
		for (Term tm1 : r1) {
			for (Term tm2 : r2) {
				if (tm1.term.equals(tm2.term)) {
					Term tmp = new Term();
					tmp.term = tm1.term;
					tmp.value = tm1.value - tm2.value;
					// Tools.debug(tm1.key + "\t" + tm2.key + "\t" + tm1.value
					// + "\t" + tm2.value);
					r.add(tm1);
					s.add(tmp.term);
				}

			}
		}
		// for (Result tmp1 : r) {
		// // Tools.debug(tmp1.key+"\t"+tmp1.value);
		// i++;
		// }
		// Tools.debug(s.size()+"\t"+i);

		// Iterator<Result> iter1 = r1.iterator();
		// Iterator<Result> iter2 = r2.iterator();
		// TreeSet<Result> r = new TreeSet<Result>();
		// Result init = new Result();
		// init.setKey("init");
		// init.setValue(0);
		// r.add(init);
		// while (iter1.hasNext()) {
		// while (iter2.hasNext()) {
		// if (iter1.next().key.equals(iter2.next().key)) {
		// Result tmp = new Result();
		// tmp.key = iter1.next().key;
		// tmp.value = iter1.next().value - iter2.next().value;
		// r.add(tmp);
		// Tools.debug(iter1.next().key + "\t" );
		// }
		//
		// }
		//
		// }
		return r;

	}

	//
	private static TreeSet<Term> FreStatic(String string)
			throws CorruptIndexException, IOException {
		// TODO Auto-generated method stub
		IndexReader ir = IndexReader.open(string);
		int n = ir.numDocs();
		TermEnum termEnum = ir.terms();
		TreeSet<Term> result = new TreeSet<Term>();
		while (termEnum.next()) {
			TermDocs termDocs = ir.termDocs(termEnum.term());
			// Tools.debug(termDocs);
			while (termDocs.next()) {
				// if termEnum.term().text() in List<term>,termDocs.freq();
				String term = termEnum.term().text();
				boolean keywordFlag = isKeywordFlag(term);
				float tf = termDocs.freq();
				float df = termEnum.docFreq();
				double weight = tf * Math.log(n / df + 0.01);
				if (weight > 1 && keywordFlag) {
					// System.out.print(term+"\t"+weight+"\t"+keywordFlag+"\n");
					Term tmp = new Term();
					tmp.term = term;
					tmp.value = df;
					result.add(tmp);
					// System.out.println("term:"+termEnum.term().text()+";weight:"+weight);
				}
			}
		}
		ir.close();
		return result;

	}

	private static void witerInSVM(TreeSet<Term> result, int flag)
			throws IOException {
		// BufferedWriter br = new BufferedWriter(new
		// FileWriter("traindata,txt",
		// true));
		int i = 0;
		for (Term tm : result.descendingSet()) {
			if (i < 100) {
				// br.write(flag + ": " + tm.value);
				System.out.println(tm.term + "\t" + tm.value);
				i++;
			}
		}
		// br.close();
		// TODO Auto-generated method stub

	}

	//
	public static boolean isKeywordFlag(String term) {
		boolean tmp = true;
		for (int i = 0; i < term.length(); i++) {
			if ((term.charAt(i) >= 'a' && term.charAt(i) <= 'z')
					|| (term.charAt(i) >= 'A' && term.charAt(i) <= 'Z')
					|| (term.charAt(i) >= '0' && term.charAt(i) <= '9')) {
				tmp = false;
				break;
			}
		}
		return tmp;
		// TODO Auto-generated method stub
	}
	//
}
